package com.parse.core;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.PaException.PaException;
import com.parse.api.Padelegate;
import com.parse.api.ParserUtils;
import com.parse.api.PaserDBI;
import com.parse.contract.Args;
import com.parse.contract.Msg;
import com.parse.domain.PaserDomin;

public class ParseDBIImpl extends PaserDBI {

	private ParserUtils parserUtils;

	private Padelegate oladelegate;// 委託者

	public ParseDBIImpl() {

		parserUtils = new ParserUtilsImpl();

	}

	@Override
	public <T extends PaserDomin> void inser(T insertObj,
			final Activity activity) {

		this.oladelegate = (Padelegate) activity;

		final ParseObject pasrserObj = this.parserUtils
				.makeParseObject(insertObj);
		pasrserObj.put(Args.USER_ID, ParseUser.getCurrentUser());
		pasrserObj.saveInBackground(new SaveCallback() {

			@Override
			public void done(ParseException e) {
				parseExceptionHander(e);
			}
		});

	}

	@Override
	public <T extends PaserDomin> void delete(String objectId, Class<T> pClass,
			Activity activity) {
		// TODO Auto-generated method stub

	}

	@Override
	public <T1 extends PaserDomin, T2 extends PaserDomin> void inserOne2Many(
			final T1 pareObj, final List<T2> sonObjs, Activity activity) {

		this.oladelegate = (Padelegate) activity;

		final ParseObject par = this.parserUtils.makeParseObject(pareObj);

		par.put("user", ParseUser.getCurrentUser());

		par.saveInBackground(new SaveCallback() {

			@Override
			public void done(ParseException e) {
				ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
						pareObj.getClass().getSimpleName());
				query.orderByDescending("createdAt");
				query.whereEqualTo("user", ParseUser.getCurrentUser());
				query.getFirstInBackground(new GetCallback<ParseObject>() {

					@Override
					public void done(ParseObject object, ParseException e) {
						if (object == null) {

						} else {

							final int totalSize = sonObjs.size();
							for (final T2 t2 : sonObjs) {
								final ParseObject son = parserUtils
										.makeParseObject(t2);
								son.put("orderId", object.getObjectId());
								son.saveInBackground(new SaveCallback() {

									@Override
									public void done(ParseException e) {
										//最後一筆才處理
										if (sonObjs.indexOf(t2) == totalSize - 1) {
											parseExceptionHander(e);
										}

									}
								});
							}
						}

					}
				});
				// TODO Auto-generated method stub

			}
		});

	}

	private void parseExceptionHander(ParseException e) {
		if (e == null) {
			oladelegate.putMessage(Args.COMMAND_CODE, "", null);
		} else {
			oladelegate.putMessage(Args.COMMAND_CODE, "", new PaException(
					Msg.PASER_ERROR001));
		}
	}

	@Override
	public <T extends PaserDomin> void inserWithoutUserId(T insertObj,
			Activity activity) {

		this.oladelegate = (Padelegate) activity;

		final ParseObject pasrserObj = this.parserUtils
				.makeParseObject(insertObj);

		pasrserObj.saveInBackground(new SaveCallback() {

			@Override
			public void done(ParseException e) {
				parseExceptionHander(e);
			}
		});

	}

	@Override
	public <T1 extends PaserDomin, T2 extends PaserDomin> void inserOne2ManyWithoutUserId(
			T1 pareObj, List<T2> sonObjs, Activity activity) {

		this.oladelegate = (Padelegate) activity;
		final ParseObject par = this.parserUtils.makeParseObject(pareObj);

		for (T2 t2 : sonObjs) {
			final ParseObject son = this.parserUtils.makeParseObject(t2);
			son.put("parent", par);

			son.saveInBackground(new SaveCallback() {

				@Override
				public void done(ParseException e) {
					parseExceptionHander(e);

				}
			});
		}

	}

}
